//
//  AppDelegate.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/9/29.
//  Copyright © 2015年 world. All rights reserved.
//

import UIKit
import AFNetworking

///全局变量记录账号信息,只加载一次用户账号,后续再使用账号,就不用从磁盘加载,直接从内存中取就可以了
///执行时机,在需要的时候才会执行,并不是程序一启动就执行
/// 全局变量的定义 要放在@UIApplicationMain的上面
var sharedUserAccount = SJUserAccount.loadAccount()

let SJSwitchRootVCNotification = "SJSwitchRootVCNotification"


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //设置网络指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        //设置网络缓存
        //内存缓存4M,磁盘缓存20M , 缓存路径nil则按照系统默认
        let urlCache = NSURLCache(memoryCapacity: 4*1024*1024, diskCapacity: 20*1024*1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
        
        //打印账号信息
        print(sharedUserAccount)
        print(SJUserAccount.accountPath)
        //设置全局外观
        setupAppearence()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultViewController()
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchWindowRootVC:", name: SJSwitchRootVCNotification, object: nil)
        
        return true
    }
    
    ///移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self, name: SJSwitchRootVCNotification, object: nil)
    }
    
    ///通知回调方法
    // 这个方法不能写成私有的
     func switchWindowRootVC(notific: NSNotification){
        print(notific)
        let isMainVC = notific.object as! Bool
        window?.rootViewController = isMainVC ? SJMainTabBarController() : SJWelcomeViewController()
    }
    
    ///window的默认控制器
    private func defaultViewController()->UIViewController{
    
        //1.判断是否登陆
        if sharedUserAccount != nil {//登陆了
            //2.是否有更新 (有->新特性 : 没有->欢迎 )
            return   isNewUpDate() ? SJNewFeatureViewController() : SJWelcomeViewController()
        
        }
        
        return SJMainTabBarController()
    }

    ///检查新版本
    private func isNewUpDate()->Bool{
        //1.获取应用程序 当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //将String -> double
        let version = Double(currentVersion)!
        print(version)
        //2.获取应用程序  之前版本
        let versionKey = "versionKery"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        print(sandBoxVersion)
        //3.将当前版本写入沙盒
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: versionKey)
        //Xcode6.0开始 就不需要同步了
//        NSUserDefaults.standardUserDefaults().synchronize()
        return version > sandBoxVersion
    }

    ///设置全局外观,全局外观只需要设置一次,全局共享, 需要尽早的设置,一般在didFinishLaunch中设置
    private func setupAppearence(){
        //导航栏
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        //tabBar
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

